<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2021/8/16
  Time: 17:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery/jquery-3.6.0.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap460/css/bootstrap.css"/>
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap460/css/bootstrap.min.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/bootstrap460/js/bootstrap.js"></script>
<%-- 引入在线的DataTable --%>
<link href="http://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js" ></script>
<div style="padding: 20px" class="container">
    <div class="mt-3 mb-3">
        <a class="btn btn-success" onclick="add()">添加</a>
    </div>
    <table class="table mt-3 mb-3" id="mytable">
        <thead>
        <tr>
            <th>楼房号</th>
            <th>楼房名</th>
            <th>房间数</th>
            <th>楼层数</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${buildings}" var="b">
            <tr id="b_${b.build_Id}">
                <td class="id">${b.build_Id}</td>
                <td class="name">${b.build_Name}</td>
                <td class="rooms">${b.room_amount}</td>
                <td class="floors">${b.floor_amount}</td>
                <td>
                    <a class="btn btn-primary" href="javascript:" onclick="edit(${b.build_Id})">修改</a>
                    <a class="btn btn-danger" href="javascript:" onclick="del(${b.build_Id})">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
    <!-- 添加用的模态框 -->
    <div class="modal fade" id="addModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title">添加楼房</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <form id="addForm" action="add" method="post" onsubmit="return false;">
                        <div class="form-group">
                            <label>楼房号</label>
                            <input type="text" id="build_id" name="build_id" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>楼房名</label>
                            <input type="text" id="build_name" name="build_name" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>房间数</label>
                            <input type="text" id="room_amount" name="room_amount" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>楼层数</label>
                            <input type="text" id="floor_amount" name="floor_amount" class="form-control">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="add_do()">确认</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑用的模态框 -->
    <div class="modal fade" id="editModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title">修改信息</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <form id="editForm" action="add" method="post" onsubmit="return false;">
                        <div class="form-group">
                            <label>楼房号</label>
                            <input type="text" id="buildId" name="buildId" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>楼房名</label>
                            <input type="text" id="buildName" name="buildName" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>房间数</label>
                            <input type="text" id="roomAmount" name="roomAmount" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>楼层数</label>
                            <input type="text" id="floorAmount" name="floorAmount" class="form-control">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="edit_do()">确认</button>
                </div>
            </div>
        </div>
    </div>

    <%--删除用的模态框--%>
    <div class="modal fade" id="deleteModal">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalCenterTitle">确定修改</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="form-group" style="display: none">
                        <label>楼房</label>
                        <input type="text" id="building_id" name="building_id" class="form-control" readonly>
                    </div>
                    <div class="form-group">
                        <label>楼房</label>
                        <input type="text" id="buildingName" name="buildingName" class="form-control" readonly>
                    </div>
                    <p>您确定要删除么？</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" onclick="del_do()">确定</button>
                </div>
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/hui/lib/datatables/1.10.15/jquery.dataTables.js">
<script type="text/javascript" charset="utf8" src="${pageContext.request.contextPath}/hui/lib/datatables/1.10.15/jquery.dataTables.min.js"></script>
<script>
    $(function(){
        $("#mytable").DataTable();
    })

    function add() {
        //弹出模态框
        $("#addModal").modal("show");
    }

    function add_do() {
        //开始发送请求    ajax({json})
        $.ajax({
            //发往何处
            url:"addBuilding",
            method:"post",
            data: {
                build_id:$("#build_id").val(),
                build_name:$("#build_name").val(),
                room_amount:$("#room_amount").val(),
                floor_amount:$("#floor_amount").val(),
            },
            success:function (data) {
                console.log(data);
                if ('ok' == data){
                    location.reload();
                }else {
                    alert("操作失败!")
                    location.reload()
                }
            },
            error:function (e) {
                console.log(e);
            }
        });
    }

    function edit(id) {
        $("#editModal").modal("show");
        $("#buildId").val($("#b_" + id + " .id").text())
        $("#buildName").val($("#b_" + id + " .name").text())
        $("#roomAmount").val($("#b_" + id + " .rooms").text())
        $("#floorAmount").val($("#b_" + id + " .floors").text())
    }

    function edit_do() {
        $.ajax({
            url:"editBuilding",
            method:"post",
            data:{
                id:$("#buildId").val(),
                name:$("#buildName").val(),
                rooms:$("#roomAmount").val(),
                floors:$("#floorAmount").val(),
            },
            success:function (data) {
                console.log(data)
                if (data == 'ok'){
                    location.reload();
                }else alert("修改失败")
            },
            error:function (e) {
                console.log(e);
                console.log("通讯错误");
            }
        })
    }

    function del(id) {
        $("#deleteModal").modal("show");
        $("#building_id").val($("#b_" + id + " .id").text())
        $("#buildingName").val($("#b_" + id + " .name").text())
    }

    function del_do() {
        $.ajax({
            url:"delBuilding",
            method:"post",
            data:{
                id:$("#building_id").val()
            },
            success:function (data) {
                console.log(data);
                if ("ok" == data){
                    location.reload();
                }else alert("删除失败");
            },
            error:function (e) {
                console.log(e);
                console.log("ajax错误")
            },
        })
    }
</script>
</body>
</html>
